<template>
  <abstract-dialog
    :loading="loading"
    :title="title"
    :value="value"
    width="80%"
    @close="cancel"
    @open="open"
  >
    <abstract-form :model="form" :rules="rules" label-width="120px">
      <el-form-item label="工作小结" prop="fileList">
        <!-- <el-input
          v-model="form.jobSummary"
          type="textarea"
          :rows="12"
          placeholder="请输入你的工作小结"
          size="small"
        ></el-input> -->
        <tinymce v-model="form.jobSummary" :height="300" />
      </el-form-item>
      <!-- 阻止回车时页面刷新 -->
      <input type="text" style="display: none" />
    </abstract-form>

    <template v-slot:footer>
      <el-button plain size="small" @click="closeDialog">取 消</el-button>
      <el-button size="small" type="primary" @click="confirm">确 定</el-button>
    </template>
  </abstract-dialog>
</template>

<script>
import AbstractDialog from '@/components/abstract/Dialog'
import dialogMixin from '@/mixin/dialogMixin'
import AbstractForm from '@/components/abstract/Form'
import Tinymce from '@/components/Tinymce'
import { elSuccess } from '@/utils/message'
import { fileUpload } from '@/api/request'

export default {
  name: 'EditDialog',

  components: {
    AbstractDialog,
    AbstractForm,
    Tinymce
  },

  mixins: [dialogMixin],

  props: {
    value: Boolean,
    data: { type: Object, default: () => ({}) },
    id: {
      type: Number,
      default: NaN
    }
  },

  data() {
    const validateFileList = (rule, value, callback) => {
      if (this.$refs.upload.uploadFiles.length === 0) {
        callback(new Error('请上传工作小结'))
      } else {
        callback()
      }
    }

    return {
      loading: false,
      title: '提交工作小结',
      form: {
        jobSummary: ''
      },
      rules: {
        fileList: [{ validator: validateFileList, trigger: 'blur' }]
      }
    }
  },

  methods: {
    open() {
      this.isUpdateData = true
    },

    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      )
    },

    clearForm() {
      this.form.fileList = []
      this.$nextTick(() => this.$refs.form.clearValidate())
    },

    cancel() {
      this.closeDialog()
      this.clearForm()
    },

    confirm() {
      this.$emit('success')
      this.cancel()
    },

    confirm2() {
      if (this.loading) return
      this.$refs.form.validate((v) => {
        if (!v) return
        this.loading = true
        const promise = fileUpload(
          'personal/myWorkSummary/upload',
          this.$refs.upload.uploadFiles[0].raw,
          {
            id: this.id
          },
          () => {}
        )
        promise
          .then(() => {
            elSuccess()
            this.$emit('success')
            this.cancel()
          })
          .finally(() => this.$nextTick(() => (this.loading = false)))
      })
    }
  }
}
</script>
